package com.wenbo.wenbo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenbo.wenbo.dto.StoryDto;
import com.wenbo.wenbo.dto.TxDataDto;
import com.wenbo.wenbo.entity.Poetry;
import com.wenbo.wenbo.entity.Story;
import com.wenbo.wenbo.mapper.PoetryMapper;
import com.wenbo.wenbo.mapper.StoryMapper;
import com.wenbo.wenbo.service.PoetryService;
import com.wenbo.wenbo.service.StoryService;
import com.wenbo.wenbo.utils.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2021年12月08日 10:03
 */
@Service
public class PoetryServiceImpl extends ServiceImpl<PoetryMapper, Poetry> implements PoetryService {

    private static final Logger log = LoggerFactory.getLogger(PoetryServiceImpl.class);
    @Value("${poetry.api}")
    private String api;
    @Value("${poetry.key}")
    private String key;


//    @Override
//    public List<StoryDto> getStory(String name, String type) {
//        LambdaQueryWrapper<Story> wrapper1 = Wrappers.lambdaQuery(Story.class);
//        wrapper1.eq(Story::getType, type);
//        wrapper1.orderByDesc(Story::getPage);
//        wrapper1.last("limit 1");
//        Story s = getOne(wrapper1);
//        Integer max = s.getPage();
//        Integer min = 1;
//        int index = (int) (Math.random() * (max - min)) + min;
//        LambdaQueryWrapper<Story> wrapper = Wrappers.lambdaQuery(Story.class);
//        wrapper.eq(Story::getType, type);
//        wrapper.eq(Story::getPage, index);
//        wrapper.like(StringUtils.isNotBlank(name), Story::getTitle, name);
//        return baseMapper.selectList(wrapper).stream().map(x -> new StoryDto(x.getId(), x.getTitle())).collect(Collectors.toList());
//    }
//
//    @Override
//    public StoryDto getStoryInfo(String id) {
//        StoryDto storyDto = new StoryDto();
//        Story story = baseMapper.selectById(id);
//        BeanUtil.copyProperties(story, storyDto);
//        return storyDto;
//    }

    @Override
    public void getPoetryList() throws InterruptedException {
        for (int i = 1; i < 2; i++) {
            getPoetry(i);
            Thread.sleep(1000);
        }
    }


    private void getPoetry(Integer page) {
        StringBuffer sb = new StringBuffer();
        sb.append("key=").append(key).append("&page=").append(page).append("&num=10");
        String res = HttpRequest.sendGet(api, sb.toString());
        log.error(res);
//        TxDataDto txDataDto = JSONUtil.toBean(res, TxDataDto.class);
//        if (txDataDto.getCode().equals("200")) {
//            List<StoryDto> list = txDataDto.getNewslist();
//            for (StoryDto storyDto : list) {
//                saveStory(storyDto, type, page);
//            }
//
//        }
    }

    private void saveStory(StoryDto storyDto, Integer type, Integer page) {
        Story s = new Story();
        s.setContent(storyDto.getContent());
        s.setTitle(storyDto.getTitle());
        s.setType(type);
        s.setPage(page);
        s.insert();
    }
}
